package com.lv.utils.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.scheduling.Trigger;
import org.springframework.scheduling.TriggerContext;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.scheduling.support.CronTrigger;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.concurrent.ScheduledFuture;

/**
 * Copyright (C), 2020-2021, 群杰印章物联网
 * FileName: com.system.utils.ScheduleTaskUtil
 *
 * @author: Lv_sc
 * Date:     2021/3/24 11:18
 * Description: //模块目的、功能描述
 * History: //修改记录
 * &lt;author&gt;      &lt;time&gt;      &lt;version&gt;    &lt;desc&gt;
 * 修改人姓名             修改时间            版本号                  描述
 */
@Component
@Configuration      //1.主要用于标记配置类，兼备Component的效果。
@EnableScheduling   // 2.开启定时任务
public abstract class ScheduleTaskUtil implements SchedulerObjectInterface {

    private static Logger logger = LoggerFactory.getLogger(ScheduleTaskUtil.class);

    //每天零点执行
    private static String cronValueFixed = "0 0 0 * * ?";

    private ScheduledFuture future;

    @Autowired
    private TaskScheduler scheduler;

    @Override
    public void start(){
        future = scheduler.schedule(new Runnable() {
            @Override
            public void run() {
                if (getStatusIsAllClose()) {
                    printRemind();
                } else {
                    doSomething();
                }
            }
        }, new Trigger() {
            @Override
            public Date nextExecutionTime(TriggerContext triggerContext) {
                String cronValue = getCronExpression();
                if (StringUtils.isEmpty(cronValue)) {
                    cronValue = cronValueFixed;
                    logger.error("========================");
                }
                logger.info("========================当前同步表达式为{}",cronValue);
                CronTrigger trigger = new CronTrigger(cronValue);
                return trigger.nextExecutionTime(triggerContext);
            }
        });
        logger.info("配置管理定时任务启动");
    }

    @Override
    public void stop(){
        future.cancel(false);
        logger.info("配置管理定时任务关闭");
    }

    @Override
    public void restart(){
        stop();
        start();
        logger.info("配置管理定时任务重启");
    }



    /**
     * @MethodName:doSomething
     * 〈功能详细描述〉配置启用时,调用接口同步数据
     * Description: DOTO
     * @Param:
     * @Author Lv_sc
     * @Data 2021/3/24 15:26
     * @Return
     */
    public abstract void doSomething();

    /**
     * @MethodName:配置停用时,打印信息
     * 〈功能详细描述〉
     * Description: DOTO
     * @Param:
     * @Author Lv_sc
     * @Data 2021/3/24 15:27
     * @Return
     */
    public void printRemind() {
        logger.info("当前无配置启用");
    }

    /**
     * @MethodName:getStatus
     * 〈功能详细描述〉获取所有配置的启用状态，并判断是否全部停用
     * Description: DOTO
     * @Param:
     * @Author Lv_sc
     * @Data 2021/3/24 11:23
     * @Return
     */
    protected abstract boolean getStatusIsAllClose();

    /**
     * @MethodName:getCronExpression
     * 〈功能详细描述〉在有开启的配置情况下，获取当前同步表达式
     * Description: DOTO
     * @Param:
     * @Author Lv_sc
     * @Data 2021/3/24 11:25
     * @Return
     */
    protected abstract String getCronExpression();

    @Bean
    public TaskScheduler scheduler() {
        return new ThreadPoolTaskScheduler();
    }

}